package dashboard;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class RegisterController {

	private User user;

	public RegisterController()
	{
		user = new User();
	}

	public List<String> setInfo(String username, String password, String cPassword, String email, String firstName, String name, String[] allCourses, String number, String role)
	{
		List<String> exception = new ArrayList<String>();
		Date now = new Date();
		user.setFirstName(firstName);

		user.setName(name);

		user.setRole(role);
		
		user.setUsername(username);

		if(UserRegistry.getInstance().getUser(username) != null)	exception.add("Username in use!");
		if(username.length()  < 6)	exception.add("No valid username!");

		user.setNumber(number);

		if(number != null && !number.startsWith("r") && !number.startsWith("u") || number.length() != 8 )
			exception.add("Student/tutor number should start with r or u and should be 8 chars long");

		user.setEmail(email);

		if(!email.contains("@") || email.split("@")[0].length()<5 || !email.contains("."))
			exception.add("E-mail is not valid!");
		if( UserRegistry.getInstance().emailInUse(email))
			exception.add("E-mail is in use");

		user.setLastLogin(now);
		user.setPreviousLastLogin(now);
		user.setPassword(password);

		if(password.length()<5)		exception.add("Password too short!");
		if(password.length()>20)	exception.add("Password too long!");
		if(!password.equals(cPassword)) exception.add("Passwords don't match");
		if(role.equals("student"))

		user.clearCourses();
		if(user.getRole() == User.Role.STUDENT){
		if(allCourses == null) exception.add("Select Courses please");
		else{
			for (int i = 0; i < allCourses.length; i++) 
			{
				user.addCourse( Long.parseLong(allCourses[i]));
			}
		}
		}
		return exception;
	}
	public User getUser()
	{
		return user;
	}
	public void saveUser()
	{
		UserRegistry.getInstance().addUser(user);
		user = new User();
	}

}
